import 'package:flutter/material.dart';

// 待办事项数据模型
class TodoItem {
  final String id;
  final String title;
  final String date;
  final String priority;
  final bool isCompleted;
  final String category;

  TodoItem({
    required this.id,
    required this.title,
    required this.date,
    required this.priority,
    required this.isCompleted,
    required this.category,
  });
}

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: '我的待办',
      theme: ThemeData(
        primarySwatch: Colors.blue,
        fontFamily: 'PingFang SC',
      ),
      home: const TodoListPage(),
    );
  }
}

class TodoListPage extends StatefulWidget {
  const TodoListPage({super.key});

  @override
  State<TodoListPage> createState() => _TodoListPageState();
}

class _TodoListPageState extends State<TodoListPage> {
  // 模拟待办事项数据
  List<TodoItem> _todos = [
    TodoItem(
      id: '1',
      title: '完成项目报告',
      date: '2023-06-15',
      priority: '高',
      isCompleted: false,
      category: '工作',
    ),
    TodoItem(
      id: '2',
      title: '购买 groceries',
      date: '2023-06-12',
      priority: '中',
      isCompleted: true,
      category: '生活',
    ),
    TodoItem(
      id: '3',
      title: '准备面试资料',
      date: '2023-06-18',
      priority: '高',
      isCompleted: false,
      category: '学习',
    ),
    TodoItem(
      id: '4',
      title: '阅读新书章节',
      date: '2023-06-14',
      priority: '低',
      isCompleted: false,
      category: '学习',
    ),
    TodoItem(
      id: '5',
      title: '健身房锻炼',
      date: '2023-06-13',
      priority: '中',
      isCompleted: false,
      category: '生活',
    ),
    TodoItem(
      id: '6',
      title: '购买生日礼物',
      date: '2023-06-20',
      priority: '中',
      isCompleted: false,
      category: '购物',
    ),
  ];

  // 当前选中的分类
  String _selectedCategory = '全部';

  // 统计数据
  final int _todayCount = 5;
  final int _weekCount = 12;
  final int _completedCount = 28;

  // 切换待办事项完成状态
  void _toggleTodoStatus(String id) {
    setState(() {
      _todos = _todos.map((todo) {
        if (todo.id == id) {
          return TodoItem(
            id: todo.id,
            title: todo.title,
            date: todo.date,
            priority: todo.priority,
            isCompleted: !todo.isCompleted,
            category: todo.category,
          );
        }
        return todo;
      }).toList();
    });
  }

  // 删除待办事项
  void _deleteTodo(String id) {
    setState(() {
      _todos = _todos.where((todo) => todo.id != id).toList();
    });
  }

  // 根据优先级获取对应的颜色
  Color _getPriorityColor(String priority) {
    switch (priority) {
      case '高':
        return Colors.red;
      case '中':
        return Colors.orange;
      case '低':
        return Colors.green;
      default:
        return Colors.grey;
    }
  }

  // 筛选待办事项
  List<TodoItem> _getFilteredTodos() {
    if (_selectedCategory == '全部') {
      return _todos;
    }
    return _todos.where((todo) => todo.category == _selectedCategory).toList();
  }

  @override
  Widget build(BuildContext context) {
    final filteredTodos = _getFilteredTodos();

    return Scaffold(
      appBar: AppBar(
        title: const Text('我的待办'),
        centerTitle: true,
        elevation: 0,
        actions: [
          IconButton(
            icon: const Icon(Icons.settings),
            onPressed: () {
              // 设置按钮点击事件
            },
          ),
        ],
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 统计卡片
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                _buildStatCard('今日待办', _todayCount),
                _buildStatCard('本周待办', _weekCount),
                _buildStatCard('已完成', _completedCount),
              ],
            ),
            const SizedBox(height: 20),

            // 分类标签
            SizedBox(
              height: 36,
              child: ListView(
                scrollDirection: Axis.horizontal,
                children: [
                  _buildCategoryTag('全部'),
                  _buildCategoryTag('工作'),
                  _buildCategoryTag('学习'),
                  _buildCategoryTag('生活'),
                  _buildCategoryTag('购物'),
                ],
              ),
            ),
            const SizedBox(height: 20),

            // 待办事项列表
            Expanded(
              child: ListView.builder(
                itemCount: filteredTodos.length,
                itemBuilder: (context, index) {
                  final todo = filteredTodos[index];
                  return _buildTodoItem(todo);
                },
              ),
            ),
          ],
        ),
      ),

      // 底部导航栏
      bottomNavigationBar: BottomNavigationBar(
        currentIndex: 0,
        items: const [
          BottomNavigationBarItem(
            icon: Icon(Icons.home),
            label: '首页',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.calendar_today),
            label: '日历',
          ),
          BottomNavigationBarItem(
            icon: Icon(Icons.person),
            label: '我的',
          ),
        ],
      ),

      // 添加待办按钮
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          // 添加新待办事项
        },
        backgroundColor: Colors.blue,
        child: const Icon(Icons.add),
      ),
      floatingActionButtonLocation: FloatingActionButtonLocation.endFloat,
    );
  }

  // 构建统计卡片
  Widget _buildStatCard(String title, int count) {
    return Expanded(
      child: Container(
        height: 80,
        margin: const EdgeInsets.symmetric(horizontal: 5),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(12),
          boxShadow: [
            BoxShadow(
              color: Colors.grey.withAlpha(25),
              spreadRadius: 2,
              blurRadius: 5,
              offset: const Offset(0, 2),
            ),
          ],
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              count.toString(),
              style: const TextStyle(
                fontSize: 24,
                fontWeight: FontWeight.bold,
                color: Colors.blue,
              ),
            ),
            Text(
              title,
              style: TextStyle(
                fontSize: 14,
                color: Colors.grey[600],
              ),
            ),
          ],
        ),
      ),
    );
  }

  // 构建分类标签
  Widget _buildCategoryTag(String category) {
    final isSelected = _selectedCategory == category;
    return GestureDetector(
      onTap: () {
        setState(() {
          _selectedCategory = category;
        });
      },
      child: Container(
        padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
        margin: const EdgeInsets.only(right: 10),
        decoration: BoxDecoration(
          color: isSelected ? Colors.blue : Colors.grey[100],
          borderRadius: BorderRadius.circular(18),
        ),
        child: Text(
          category,
          style: TextStyle(
            color: isSelected ? Colors.white : Colors.black87,
            fontSize: 14,
            fontWeight: FontWeight.w500,
          ),
        ),
      ),
    );
  }

  // 构建待办事项项
  Widget _buildTodoItem(TodoItem todo) {
    return Container(
      margin: const EdgeInsets.only(bottom: 12),
      padding: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(12),
        boxShadow: [
          BoxShadow(
              color: Colors.grey.withAlpha(25),
              spreadRadius: 2,
              blurRadius: 5,
              offset: const Offset(0, 2),
            ),
        ],
      ),
      child: Row(
        children: [
          // 复选框
          Checkbox(
            value: todo.isCompleted,
            onChanged: (value) {
              _toggleTodoStatus(todo.id);
            },
            activeColor: Colors.blue,
          ),
          const SizedBox(width: 12),

          // 待办信息
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  todo.title,
                  style: TextStyle(
                    fontSize: 16,
                    fontWeight: FontWeight.w500,
                    decoration: todo.isCompleted
                        ? TextDecoration.lineThrough
                        : TextDecoration.none,
                    color: todo.isCompleted ? Colors.grey[600]! : Colors.black87,
                  ),
                ),
                const SizedBox(height: 4),
                Row(
                  children: [
                    Text(
                      todo.date,
                      style: TextStyle(
                        fontSize: 12,
                        color: Colors.grey[600],
                      ),
                    ),
                    const SizedBox(width: 12),
                    Container(
                      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 2),
                      decoration: BoxDecoration(
                        color: _getPriorityColor(todo.priority).withAlpha(25),
                        borderRadius: BorderRadius.circular(4),
                      ),
                      child: Text(
                        todo.priority,
                        style: TextStyle(
                          fontSize: 10,
                          color: _getPriorityColor(todo.priority),
                          fontWeight: FontWeight.w500,
                        ),
                      ),
                    ),
                  ],
                ),
              ],
            ),
          ),

          // 删除按钮
          IconButton(
            icon: const Icon(Icons.delete_outline),
            color: Colors.grey,
            onPressed: () {
              _deleteTodo(todo.id);
            },
          ),
        ],
      ),
    );
  }
}
